﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
//from:http://i.aspx1.com
namespace GetNewsForm
{
    public class Rss
    {
        public static DataTable GetChannel()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("NewsID");
            dt.Columns.Add("NewsName");
            dt.Columns.Add("NewsUrl");
            dt.Columns.Add("StartTag");
            dt.Columns.Add("EndTag");
            dt.Columns.Add("ImgStartTag");
            dt.Columns.Add("ImgEndTag");
            dt.Columns.Add("IsGetNews");
            dt.Columns.Add("NewsDescription");
            dt.Columns.Add("MaxNewscount");
            XmlDocument XML = new XmlDocument();
            XML.Load("RssNews.xml");
            XmlNodeList NodeList = XML.SelectNodes("RssNews/NewsSite/Channel");
            foreach (XmlNode Node in NodeList)
            {
                DataRow row = dt.Rows.Add();
                row["NewsName"] = Node["NewsName"].InnerText;
                row["NewsUrl"] = Node["NewsUrl"].InnerText;
                row["StartTag"] = Node["StartTag"].InnerText;
                row["EndTag"] = Node["EndTag"].InnerText;
                row["ImgStartTag"] = Node["ImgStartTag"].InnerText;
                row["ImgEndTag"] = Node["ImgEndTag"].InnerText;
                row["IsGetNews"] = Node["IsGetNews"].InnerText;
                row["NewsDescription"] = Node["NewsDescription"].InnerText;
                row["NewsID"] = Node["NewsID"].InnerText;
                row["MaxNewscount"] = Node["MaxNewscount"].InnerText;

            }
            return dt;
        }
        public static void UpdateRssNews(RssModel rss)
        {
            XmlDocument XML = new XmlDocument();
            XML.Load("RssNews.xml");
            XmlNode Node = XML.SelectSingleNode("RssNews/NewsSite/Channel[NewsUrl='" + rss.NewsUrl + "']");
            // XmlNode Node = Node1.ParentNode;
            Node["NewsName"].InnerText = rss.NewsName;
            Node["NewsUrl"].InnerText = rss.NewsUrl;
            Node["StartTag"].InnerText = rss.StartTag;
            Node["EndTag"].InnerText = rss.EndTag;
            Node["IsGetNews"].InnerText = rss.IsGetNews;
            Node["ImgStartTag"].InnerText = rss.ImgStartTag;
            Node["ImgEndTag"].InnerText = rss.ImgEndTag;
            Node["NewsDescription"].InnerText = rss.NewsDescription;
            Node["NewsID"].InnerText = rss.NewsID;
            Node["MaxNewscount"].InnerText = rss.MaxNewscount;
            XML.Save("RssNews.xml");
        }
        public static void DeleteRssNews(RssModel rss)
        {
            XmlDocument XML = new XmlDocument();
            XML.Load("RssNews.xml");
            string NewsUrl = rss.NewsUrl;
            XmlNode Node = XML.SelectSingleNode("RssNews/NewsSite/Channel[NewsUrl='" + rss.NewsUrl + "']");
            if (Node != null)
            {
                Node.ParentNode.RemoveChild(Node);
                XML.Save("RssNews.xml");
            }
        }
        public static void AddSite(RssModel rss)
        {
            XmlDocument XML = new XmlDocument();
            XML.Load("RssNews.xml");
            XmlNode Node = XML.SelectSingleNode("RssNews/NewsSite");
            XmlElement Channel = XML.CreateElement("Channel");
            XmlElement NewsName = XML.CreateElement("NewsName");
            XmlElement MaxNewscount = XML.CreateElement("MaxNewscount");
            XmlElement NewsUrl = XML.CreateElement("NewsUrl");
            XmlElement StartTag = XML.CreateElement("StartTag");
            XmlElement EndTag = XML.CreateElement("EndTag");
            XmlElement ImgStartTag = XML.CreateElement("ImgStartTag");
            XmlElement ImgEndTag = XML.CreateElement("ImgEndTag");
            XmlElement IsGetNews = XML.CreateElement("IsGetNews");
            XmlElement NewsDescription = XML.CreateElement("NewsDescription");
            XmlElement NewsID = XML.CreateElement("NewsID");
            XmlElement ConfigNode = XML.CreateElement("Config");
            XmlElement IsGetA = XML.CreateElement("IsGetA");
            XmlElement IsGetTABLE = XML.CreateElement("IsGetTABLE");
            XmlElement IsGetSPAN = XML.CreateElement("IsGetSPAN");
            XmlElement IsGetDIV = XML.CreateElement("IsGetDIV");
            XmlElement IsGetIMG = XML.CreateElement("IsGetIMG");
            IsGetA.InnerText = "FALSE";
            IsGetDIV.InnerText = "FALSE";
            IsGetIMG.InnerText = "FALSE";
            IsGetSPAN.InnerText = "FALSE";
            IsGetTABLE.InnerText = "FALSE";
            ConfigNode.AppendChild(IsGetA);
            ConfigNode.AppendChild(IsGetIMG);
            ConfigNode.AppendChild(IsGetDIV);
            ConfigNode.AppendChild(IsGetSPAN);
            ConfigNode.AppendChild(IsGetTABLE);            
            NewsName.InnerText = rss.NewsName;
            MaxNewscount.InnerText = "10";
            NewsUrl.InnerText = rss.NewsUrl;
            StartTag.InnerText = rss.StartTag;
            EndTag.InnerText = rss.EndTag;
            ImgStartTag.InnerText = rss.ImgStartTag;
            ImgEndTag.InnerText = rss.ImgEndTag;
            IsGetNews.InnerText = rss.IsGetNews;
            NewsDescription.InnerText = rss.NewsDescription;
            NewsID.InnerText = rss.NewsID;
            Channel.AppendChild(NewsDescription);
            Channel.AppendChild(NewsName);
            Channel.AppendChild(MaxNewscount);
            Channel.AppendChild(NewsID);
            Channel.AppendChild(StartTag);
            Channel.AppendChild(ImgStartTag);
            Channel.AppendChild(NewsUrl);
            Channel.AppendChild(IsGetNews);
            Channel.AppendChild(EndTag);
            Channel.AppendChild(ImgEndTag);
            Channel.AppendChild(ConfigNode);
            Node.AppendChild(Channel);
            XML.Save("RssNews.xml");

        }
        public static bool ExistNews(RssModel rss)
        {
            XmlDocument XML = new XmlDocument();
            XML.Load("RssNews.xml");
            string NewsUrl = rss.NewsUrl;
            XmlNode Node = XML.SelectSingleNode("RssNews/NewsSite/Channel[NewsUrl='" + rss.NewsUrl + "']");
            if (Node == null)
                return false;
            else
                return true;
        }
    }
}
